package com.sy.controller;

import com.alibaba.fastjson.JSON;
import com.sy.bean.JwtOperation;
import com.sy.constant.Method;
import com.sy.dto.JsonResult;
import com.sy.dto.Menu;
import com.sy.dto.UserDto;
import com.sy.entity.UserPermit;
import com.sy.exception.ParamException;
import com.sy.exception.PermissionException;
import com.sy.repository.ModelPageRepository;
import com.sy.repository.UserPermitRepository;
import com.sy.utils.HttpUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.sy.constant.BusinessConstant.BASE_URL;

/**
 * @author zbbstart
 * @date 2021/1/30 - 21:21
 */
@RestController
@RequestMapping("menus")
public class MenuController {
    @Autowired
    private JwtOperation jwtOperation;
    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private ModelPageRepository modelPageRepository;
    @Autowired
    private UserPermitRepository userPermitRepository;

    @GetMapping
    public JsonResult getMenuList(@RequestHeader("auth-token") String token, HttpServletResponse res) {
        JsonResult result = new JsonResult();
        List<Menu> menuList = new ArrayList<>();

        //获取Claims
        Claims claims = jwtOperation.parseJwt(token);
        //获取用户信息
        UserDto user = JSON.parseObject(String.valueOf(claims.get("user")), UserDto.class);
        List<String> roles = user.getRoles();
        List<String> list = modelPageRepository.getMenuList();
        List<String> jsList = modelPageRepository.getJsList();
        List<String> menulink = list.stream().map(l -> BASE_URL + l).collect(Collectors.toList());
        if (roles.contains("ROLE_ADMIN")) {
            menuList.add(new Menu(1,"用户管理",menulink.get(1),jsList.get(1)));
            menuList.add(new Menu(2,"问卷管理",menulink.get(2),jsList.get(2)));
        }else {
            UserPermit userPermit = userPermitRepository.findAllByUserId(Integer.parseInt(user.getUserId()));
            menuList.add(new Menu(1,"问卷管理",menulink.get(2),jsList.get(2)));
//            System.out.println(userPermit.getUpdatePwd());
            if (userPermit.getUpdatePwd() == 1){
                menuList.add(new Menu(1,"修改密码",menulink.get(0),jsList.get(0)));
            }
        }
        result = httpUtils.buildSuccess(Method.GET,menuList);
        return result;
    }

    /**
     * 获取用户管理界面
     */
    @GetMapping("userManager")
    public JsonResult userManagerMenu(){
        String userManagerHtml = modelPageRepository.getHtmlList().get(1);
        return httpUtils.buildSuccess(Method.GET,userManagerHtml);
    }

    /**
     * 获取修改密码界面
     */
    @GetMapping("updatePwd")
    public JsonResult updatePwdMenu(){
        String userManagerHtml = modelPageRepository.getHtmlList().get(0);
        return httpUtils.buildSuccess(Method.GET,userManagerHtml);
    }

    /**
     * 获取问卷管理界面
     */
    @GetMapping("questionnaire")
    public JsonResult questionnaireMenu(){
        String userManagerHtml = modelPageRepository.getHtmlList().get(2);
        return httpUtils.buildSuccess(Method.GET,userManagerHtml);
    }

}
